import {
  request
} from '../../utils/request'
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    name: '',
    introduction: '',
    imgUrl: '',
    tags: '',
    typeList: [{
        label: '活动',
        key: 'ACTIVITY'
      },
      {
        label: '课程',
        key: 'COURSE'
      },
      {
        label: '比赛',
        key: 'MATCH'
      }
    ],
    typeCode: '',
    typeName: '',
    size: {
      //裁剪尺寸
      width: 160,
      height: 160
    },
    cropSizePercent: 0.6, //裁剪框显示比例
    borderColor: '#fff' //裁剪框边框颜色
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  selectConfirm(detail) {
    this.setData({
      typeCode: detail.detail.selectItem
    })
    this.setData({
      typeName: detail.detail.selectLabel
    })
  },

  /**
   * 提交表单
   */
  createEvent() {
    if (this.data.imgUrl.length <= 0) {
      wx.showToast({
        title: '请选择队伍头像',
        icon: 'none'
      })
      return
    }
    if (this.data.name.length <= 0) {
      wx.showToast({
        title: '请输入队伍名称',
        icon: 'none'
      })
      return
    }
    if (this.data.typeName.length <= 0) {
      wx.showToast({
        title: '请选择队伍分类',
        icon: 'none'
      })
      return
    }
    if (
      this.data.typeCode == 'OTHER' &&
      this.data.otherName.length <= 0
    ) {
      wx.showToast({
        title: '请输入其他分类名称',
        icon: 'none'
      })
      return
    }
    if (this.data.introduction.length <= 0) {
      wx.showToast({
        title: '请输入队伍介绍',
        icon: 'none'
      })
      return
    }
    request({
      url: '/mini/api/selectTeam/groupTeamSave',
      data: {
        name: this.data.name,
        type: this.data.typeCode,
        otherType: this.data.otherName,
        location: this.data.location,
        introduction: this.data.introduction,
        avatar: this.data.imgUrl,
        tags: this.data.tags
      },
      method: 'POST'
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.showToast({
          title: '创建成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 200)
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  deleteImg() {
    this.setData({
      fileList: []
    })
    this.setData({
      imgUrl: ''
    })
  },

  uploadImg(image) {
    var _this = this
    const token = wx.getStorageSync('token')
    wx.uploadFile({
      url: 'https://elf.byxfd.com' +
        '/mini/api/file/upload',
      header: {
        'content-type': 'multipart/form-data',
        Authorization: token || ''
      },
      filePath: image,
      name: 'file',
      success(res) {
        let data = res.data
        if (data) {
          data = JSON.parse(data)
        }
        if (data.code == 200) {
          let fileList = [{
            url: data.data.fileUrl,
            thumb: data.data.fileUrl,
            status: 'success',
            isImage: true,
            name: 'img',
            id: '1',
            type: 'image'
          }]
          _this.setData({
            fileList: fileList,
            imgUrl: data.data.fileUrl
          })
        }
      },
      fail(err) {
        console.log(err)
      }
    })
  },

  /**
   * 上传图片
   * @param {*} event
   */
  afterRead(event) {
    const {
      file
    } = event.detail
    const image = file.tempFilePath
    wx.navigateTo({
      url: '/packageMy/croper/croper?imgSrc=' +
        image +
        '&size=' +
        JSON.stringify(this.data.size)
    })
  },

  toTemplace() {
    wx.navigateTo({
      url: '/packagePlan/imgTemplate/imgTemplate?from=' +
        'avatar'
    })
  },

  toTemplate() {
    wx.navigateTo({
      url: '/packagePlan/imgTemplate/imgTemplate?from=' +
        'activityDetail&index=' +
        '2'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (app.globalData.imgSrc.length > 0) {
      this.uploadImg(app.globalData.imgSrc)
      app.globalData.imgSrc = ''
    }
    if (this.data.imgUrl.length > 0) {
      let fileList = [{
        url: this.data.imgUrl,
        thumb: this.data.imgUrl,
        status: 'success',
        isImage: true,
        name: 'img',
        id: '1',
        type: 'image'
      }]
      this.setData({
        fileList: fileList
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})